package com.hy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.hy.base.ApiResult;
import com.hy.form.input.GazetteerAddForm;
import com.hy.form.query.GazetteerQueryForm;
import com.hy.form.result.GazetteerResultForm;
import com.hy.model.Gazetteer;
import com.hy.service.IGazetteerService;

@RestController
@RequestMapping("/gazetteer")
@Validated
public class GazetteerController {

	static final Logger logger = LoggerFactory.getLogger(GazetteerController.class);
	
	@Autowired
	IGazetteerService gazetteerService;
	
	/**
	 * 新增名录
	 * @param gazetteerAddForm
	 * @return
	 */
	@RequestMapping("/add")
    public ApiResult add(@Valid @RequestBody GazetteerAddForm gazetteerAddForm){
		gazetteerService.add(gazetteerAddForm);
		return ApiResult.SUCCESS();
	}
	
	/**
	 * 修改名录
	 * @param gazetteerAddForm
	 * @return
	 */
	@RequestMapping("/update")
    public ApiResult update(@Valid @RequestBody GazetteerAddForm gazetteerAddForm){
		gazetteerService.update(gazetteerAddForm);
		return ApiResult.SUCCESS();
	}
	
	/**
	 * 查找名录信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/queryById")
    public ApiResult queryById(@NotBlank(message = "id不能为空！") String id){
		Gazetteer gazetteer = gazetteerService.selectByPrimaryKey(id);
		return ApiResult.SUCCESS(gazetteer);
	}
	
	/**
	 * 删除名录
	 * @param id
	 * @return
	 */
	@RequestMapping("/del")
    public ApiResult del(@NotBlank(message = "id不能为空！") String id){
		gazetteerService.del(id);
		return ApiResult.SUCCESS();
	}
	
	@RequestMapping("/page")
	public ApiResult page(@Valid GazetteerQueryForm gazetteerQueryForm){
		PageInfo<GazetteerResultForm> page = gazetteerService.page(gazetteerQueryForm);
		return ApiResult.SUCCESS(page);
	}
	
	@RequestMapping("/page2")
	public PageInfo page2(@Valid GazetteerQueryForm gazetteerQueryForm){
		PageInfo<GazetteerResultForm> page = gazetteerService.page(gazetteerQueryForm);
		return page;
	}
	
	@RequestMapping("/page3")
	public Map page3(@Valid GazetteerQueryForm gazetteerQueryForm){
		PageInfo<GazetteerResultForm> page = gazetteerService.page(gazetteerQueryForm);
		Map map = new HashMap();
		map.put("count", page.getTotal());
		map.put("data", page.getList());
		return map;
	}
}
